﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SchoolManagementSystem
{
    public partial class SPPEdit : Form
    {
        private SPP parent = null;
        private DataTable dTable = null;
        private int rowIndex = -1;
        private int spp_id = -1;

        public SPPEdit()
        {
            InitializeComponent();
        }

        public SPPEdit(SPP parent_instance, DataTable spp_dataTable, int editRowIndex,
            string spp_id,
            string spp_stu_id,
            string spp_stu_name,
            object spp_year,
            object spp_month,
            string spp_description,
            object spp_amount)
        {
            InitializeComponent();

            int year = DateTime.Now.Year;
            for (int i = year; i > year - 7; i--)
            {
                cmbTahun.Items.Add(i.ToString());
            }

            this.txtInduk.Text = spp_stu_id;
            this.txtNama.Text = spp_stu_name;
            this.txtKeterangan.Text = spp_description;
            
            decimal value = (decimal)spp_amount;
            int value_int = Convert.ToInt32(value);
            this.txtJumlah.Text = value_int.ToString();

            int selectedMonth = Convert.ToInt32(spp_month);
            selectedMonth -= 1;
            this.cmbBulan.SelectedIndex = selectedMonth;

            for (int i = 0; i < cmbTahun.Items.Count; i++)
            {

                if (spp_year.ToString() == cmbTahun.Items[i].ToString())
                {
                    cmbTahun.SelectedIndex = i;
                    break;
                }
            }

            this.parent = parent_instance;
            this.dTable = spp_dataTable;
            this.rowIndex = editRowIndex;

            this.spp_id = int.Parse(spp_id);
        }

        private void SPPEdit_Load(object sender, EventArgs e)
        {
            
        }

        private void btnBatal_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSimpan_Click(object sender, EventArgs e)
        {
            if (true == this.checkFieldValue())
            {
                string command = "SELECT * FROM SPP WHERE id='" + this.spp_id.ToString() + "'";
                DataTable dt = Database.getInstance().query(command);

                if (dt.Rows.Count == 1)
                {
                    dt.Rows[0]["year"] = int.Parse(this.cmbTahun.SelectedItem.ToString());
                    dt.Rows[0]["month"] = int.Parse(this.cmbBulan.SelectedItem.ToString());
                    dt.Rows[0]["amount"] = decimal.Parse(this.txtJumlah.Text);
                    dt.Rows[0]["remaining"] = decimal.Parse(this.txtJumlah.Text);
                    dt.Rows[0]["description"] = this.txtKeterangan.Text;

                    Database.getInstance().updateDB(dt);
                    this.parent.refreshTableData();

                    var result = MessageBox.Show("Edit SPP berhasil !", "Sukses !", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    if (result == System.Windows.Forms.DialogResult.OK)
                    {
                        this.Close();
                    }
                } 
            }
        }

        private Boolean checkFieldValue()
        {
            if (this.cmbBulan.SelectedIndex == -1)
            {
                MessageBox.Show("Pilih bulan !", "Peringatan!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return false;
            }

            if (this.cmbTahun.SelectedIndex == -1)
            {
                MessageBox.Show("Pilih tahun !", "Peringatan!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return false;
            }

            if (this.txtJumlah.Text == "")
            {
                MessageBox.Show("Harap masukkan data jumlah !", "Peringatan!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return false;
            }

            return true;
        }
    }
}
